package com.mc.metaSearch;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Search360 {
	final String prefix = "http://so.360.cn/s?q=";

	final String postfix = "&src=so_iehome&ie=utf-8";// sr=0&cl=2&rn=20&ct=1&prevct=no

	public String[] getContent(String querystring) throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append(prefix).append(URLEncoder.encode(querystring, "utf-8"))
				.append(postfix);// "GBK"
		URL l = new URL(sb.toString());
		// l.openConnection();
		HttpURLConnection con = (HttpURLConnection) l.openConnection();
		con.setConnectTimeout(10000);
		con.setReadTimeout(18000);
		BufferedReader r = new BufferedReader(new InputStreamReader(
				l.openStream(), "utf-8"));// "GBK"
		StringBuffer buf = new StringBuffer();
		Pattern p = Pattern
				.compile("<li  class=\"res-list\"><h3 class=\"res-title \">.*<br></p>");
		List<String> result = new ArrayList<String>();
		String str = null;
		while ((str = r.readLine()) != null) {
			buf.append(str);
		//	System.out.println(str);
			str = null;
		}
		Matcher m = p.matcher(buf);
		if(m.find()){
			String []src=m.group().split("<li  class=\"res-list\"><h3 class=\"res-title \">");
			for(String i:src){
				String content = i.replaceAll("</a></li>", "");
				if (!(content.isEmpty() || content.equals(""))) {
					result.add(content);
				}
			}
		}
	/*	if (m.find()) {
			String[] src = m.group().split("<td class=\"f\">.*<a href=\"");
			
			for (String i : src) {
				String content = i.replaceAll("</td></tr></table><br>.*", "");
				if (!(content.isEmpty() || content.equals(""))) {
					result.add("<td class=\"text\"><a href=\"" + content);
				}
			}
		}*/
		return (String[]) result.toArray(new String[result.size()]);
	}

	public Map<Integer, String> getURL(String[] content) throws Exception {
		Pattern p = Pattern
				.compile("<a href=.*data-tp");
		Map<Integer, String> urls = new HashMap<Integer, String>();
		int k = 0;
		for (String i : content) {
			Matcher m = p.matcher(i);
			if (m.find()) {
				String[] src = m.group().split("<a href=");
				for (String url : src) {
					String url1 = url.replaceAll("target=\"_blank\".*"," ").replaceAll("href=\"","").replaceAll("\"","").replaceAll(">.*","")
							.replaceAll("data-tp", "");
					if (!(url1.isEmpty() || url1.equals("")))
					{
						urls.put(k, url1);
					}
				}
			}
			k++;
		}
		return (urls);

	}

	public Map<Integer, String> getTitle(String[] content) throws Exception {
		Pattern p_title = Pattern.compile("target=\"_blank\">.*</a></h3>");
		Map<Integer, String> titles = new HashMap<Integer, String>();
		int k = 0;
		for (String i : content) {
			Matcher m = p_title.matcher(i);
			if (m.find()) {
				String[] src = m.group().split("target=\"_blank\">");

				for (String title1 : src) {
					String title = title1.replaceAll("</a></h3>.*", "")
							.replaceAll("<[^>]*>", "")
							.replaceAll("[(/>)<]", "");

					if (!(title.isEmpty() || title.equals(""))) {
						titles.put(k, title);
						// break;
					}
				}
			}
			k++;
		}

		return (titles);
	}

	public Map<Integer, String> getAbstract(String[] content) throws Exception {

		Pattern p_abstract = Pattern.compile("<p class=\"res-desc\">.*<br></p>");

		Map<Integer, String> abstracts = new HashMap<Integer, String>();
		int k = 0;
		for (String i : content) {
			Matcher m = p_abstract.matcher(i);
			if (m.find()) {
				String[] src = m.group().split("<p class=\"res-desc\">");

				for (String abstract1 : src) {
					String abstract_ = abstract1
							.replaceAll("��������*", "")
							.replaceAll("<[^>]*>", "")
							.replaceAll("[(/>)<]", "")
							.replaceAll("�ٶȿ���", " ").replaceAll("&nbsp;", "");
					;
					if (!(abstract_.isEmpty() || abstract_.equals("")))
						abstracts.put(k, abstract_);
				}
			}
			k++;
		}
		return (abstracts);
	}

/*	public static void main(String[] args) throws UnsupportedEncodingException {
		Search360 b = new Search360();
		String[] r;
		Map<Integer, String> urls;
		Map<Integer, String> abstracts;
		Map<Integer, String> titles;
		try {
			r = b.getContent("л��");
			urls = b.getURL(r);
			abstracts = b.getAbstract(r);
			titles = b.getTitle(r);
			Iterator it=abstracts.keySet().iterator();
			int num=0;
			String url="";
			String title="";
			String abstra="";
			while(it.hasNext()){
				num=Integer.parseInt(it.next().toString());
				url=urls.get((Object)num);
				title=titles.get((Object)num);
				abstra=abstracts.get((Object)num);
				System.out.println(url+"   "+ title+"                     "+abstra);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
}
